package cn.controller;

import cn.entity.Result;
import cn.entity.SysRole;
import cn.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    SysRoleService sysRoleService;

    @RequestMapping("")
    public ModelAndView index(){
        return new ModelAndView("role/role");
    }

    @RequestMapping("saveView")
    public ModelAndView saveView(){
        return new ModelAndView("role/role-save");
    }

    @RequestMapping("select/{pageNum}/{pageSize}")
    public Result select(@PathVariable("pageNum")int pageNum,@PathVariable("pageSize")int pageSize,@RequestParam Map<String,Object> params){
        return new Result(true,"查询成功",sysRoleService.selectPage(pageNum,pageSize,params));
    }

    @RequestMapping("toRoleUser")
    public ModelAndView toRoleUser(){
        return new ModelAndView("role/role-user");
    }

    @RequestMapping("selectFromRole/{id}")
    public Result selectFromRole(@PathVariable("id")Long id){
        return new Result(true,"查询成功",sysRoleService.selectFromRole(id));
    }

    @RequestMapping("selectElse/{uid}/{oid}")
    public Result selectElse(@PathVariable("uid")Long uid,@PathVariable("oid")Long oid){
        return new Result(true,"查询成功",sysRoleService.selectElse(uid,oid));
    }

    @RequestMapping("addBtnSubmit")
    public Result addBtnSubmit(Long rid, Long uids){
        return new Result(true,"添加成功",sysRoleService.addBtnSubmit(rid, Collections.singletonList(uids)));
    }
    @RequestMapping("removeBtnSubmit")
    public Result removeBtnSubmit(Long rid, Long uids){
        return new Result(true,"移除成功",sysRoleService.removeBtnSubmit(rid, Collections.singletonList(uids)));
    }

    @RequestMapping("updateResourceAndOffice")
    public int updateResourceAndOffice(@RequestBody Map<String, Object> map){
        return sysRoleService.updateByPrimaryKeySelective(map);
    }
    @RequestMapping("selectOfficeByRid")
    public List<Long> selectOfficeByRid(Long rid){
        return sysRoleService.selectOfficeByRid(rid);
    }

}
